﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TextTemplating;
using Lilium.T4.Hosting;

namespace Lilium.Web.Mvc.T4
{
    public sealed class HttpHeaderProcessor : DirectiveProcessor
    {
        public HttpHeaderProcessor()
        {
            m_CustomDirectiveProcessors = new Dictionary<string, ICustomDirectiveProcessor>(StringComparer.OrdinalIgnoreCase);

            CustomDirectiveProcessors.Add("contentType", new ContentTypeProcessor());
            CustomDirectiveProcessors.Add("contentDisposition", new ContentDispositionProcessor());
        }

        public override void FinishProcessingRun()
        {
        }

        public override string GetClassCodeForProcessingRun()
        {
            return null;
        }

        public override string[] GetImportsForProcessingRun()
        {
            return null;
        }

        public override string GetPostInitializationCodeForProcessingRun()
        {
            return null;
        }

        public override string GetPreInitializationCodeForProcessingRun()
        {
            return null;
        }

        public override string[] GetReferencesForProcessingRun()
        {
            return null;
        }

        public override bool IsDirectiveSupported(string directiveName)
        {
            return CustomDirectiveProcessors.ContainsKey(directiveName);
        }

        public override void ProcessDirective(string directiveName, IDictionary<string, string> arguments)
        {
            ICustomDirectiveProcessor customDirectiveProcessor;
            if (CustomDirectiveProcessors.TryGetValue(directiveName, out customDirectiveProcessor))
                customDirectiveProcessor.ProcessDirective(arguments);
            else
                throw new InvalidOperationException(string.Format("Directive {0} not supported by {1}", directiveName, this.GetType().Name));
        }

        #region private IDictionary<string, ICustomDirectiveProcessor> CustomDirectiveProcessors

        private readonly IDictionary<string, ICustomDirectiveProcessor> m_CustomDirectiveProcessors;

        private IDictionary<string, ICustomDirectiveProcessor> CustomDirectiveProcessors
        {
            get
            {
                return m_CustomDirectiveProcessors;
            }
        }

        #endregion
    }
}